package com.shopping.www.service;

import com.shopping.www.mapper.DeliveryMapper;
import com.shopping.www.model.Brand;
import com.shopping.www.model.BrandAndNum;
import com.shopping.www.model.BrandForDelivery;
import com.shopping.www.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;

public class DeliveryService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public BrandAndNum<Brand> selectDeliveryBrands() {
        SqlSession sqlSession=factory.openSession();
        DeliveryMapper mapper =sqlSession.getMapper(DeliveryMapper.class);
        int[] ids=mapper.selectDeliveryIds();
        BrandAndNum<Brand> brandAndNum=new BrandAndNum<>();
        if(ids.length>0) {
            int[] userIds=mapper.selectDeliveryUserId(ids);
            int[] nums=mapper.selectDeliveryNums(ids);
            List<Brand> brands=mapper.selectDeliveryBrands(ids);
            List<Brand> brandList=new ArrayList<>();
            for (int i = 0; i < ids.length; i++) {
                for(int j=0;j<brands.size();j++) {
                    if (brands.get(j).getId() == ids[i]) {
                        brandList.add(brands.get(j));
                    }
                }
            }
            brandAndNum.setBrands(brandList);
            brandAndNum.setIds(userIds);
            brandAndNum.setNums(nums);
            sqlSession.close();
            return brandAndNum;
        }else{
            sqlSession.close();
            return brandAndNum;
        }
    }
    public Boolean checkDelivery(int delivery_id,int brand_id){
        SqlSession sqlSession=factory.openSession();
        DeliveryMapper mapper =sqlSession.getMapper(DeliveryMapper.class);
        int id=mapper.checkDelivery(delivery_id,brand_id);
        if(id==-1){
            return true;
        }else{
        return false;
        }
    }
    public void takeOrder(int delivery_id, BrandForDelivery brandForDelivery){
        SqlSession sqlSession=factory.openSession();
        DeliveryMapper mapper =sqlSession.getMapper(DeliveryMapper.class);
        mapper.takeOrder1(delivery_id,brandForDelivery);
        int id=brandForDelivery.getDeliveryUserId();
        mapper.takeOrder2(id);
        sqlSession.commit();
        sqlSession.close();
    }
    public BrandAndNum<Brand> selectWaitDeliveryBrands(int delivery_id) {
        SqlSession sqlSession = factory.openSession();
        DeliveryMapper mapper = sqlSession.getMapper(DeliveryMapper.class);
        int[] brandIds = mapper.selectWaitDeliveryBrandIds(delivery_id);
        BrandAndNum<Brand> brandAndNum = new BrandAndNum<>();
        if (brandIds.length > 0) {
            int[] deliveryIds = mapper.selectWaitDeliveryId(brandIds,delivery_id);
            int[] nums = mapper.selectWaitDeliveryNums(brandIds);
            List<Brand> brands = mapper.selectDeliveryBrands(brandIds);
            List<Brand> brandList = new ArrayList<>();
            for (int i = 0; i < brandIds.length; i++) {
                for (int j = 0; j < brands.size(); j++) {
                    if (brands.get(j).getId() == brandIds[i]) {
                        brandList.add(brands.get(j));
                    }
                }
            }
            brandAndNum.setBrands(brandList);
            brandAndNum.setIds(deliveryIds);
            brandAndNum.setNums(nums);
            sqlSession.close();
            return brandAndNum;
        } else {
            sqlSession.close();
            return brandAndNum;
        }
    }
    public void arriveDelivery(int delivery_id,BrandForDelivery brandForDelivery){
        SqlSession sqlSession=factory.openSession();
        DeliveryMapper mapper =sqlSession.getMapper(DeliveryMapper.class);
        int id=brandForDelivery.getDeliveryUserId();
        mapper.arriveDelivery1(id);
        mapper.arriveDelivery2(brandForDelivery,id);
        sqlSession.commit();
        sqlSession.close();
    }
}
